package com.example.rabbitmq.demo4_priority_queue;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;

/**
 * @Author songyahui
 * @Date 2023/7/13 9:45
 * @Description
 */
@RestController
@RequestMapping("/priority")
public class PrioritySendMsgController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMsg/{message}")
    private String sendMsg(@PathVariable("message") String message){
        MessageProperties messageProperties = new MessageProperties();
        Message msg = null;
        for(int i=1; i<11; i++){
            if(i==5){
                //优先级以小为优先
                messageProperties.setPriority(1);
            }else {
                messageProperties.setPriority(10);
            }
            msg = new Message((message+":"+i).getBytes(StandardCharsets.UTF_8), messageProperties);
            rabbitTemplate.convertAndSend("","priority_queue",msg.getBody());
        }

        return "发布成功！！！";
    }
}
